package me.qi.kancha.controller.base;

import me.qi.kancha.resp.FieldErrorException;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sun
 * @date 2022/1/5 15:34
 * @modified By：
 */
public class BaseController {

    protected static final String ID_REGX = "^[A-Za-z0-9]{1,32}$";

    /**
     * 检查是否有字段校验失败
     *
     * @param result
     */
    protected void checkBindingResult(BindingResult result) {
        if (result.hasErrors()) {
            throw new FieldErrorException(result.getFieldErrors());
        }
    }

    protected ResponseEntity build(boolean result, String errorMessage) {
        if (result) {
            return ResponseEntity.ok().build();
        } else {
            throw new RuntimeException(errorMessage);
        }
    }

//    protected ResponseEntity resp(boolean result, String successMessage, String failMessage) {
//        ResponseEntity.ok().body();
//
//    }

    protected ResponseEntity success(Object data) {
        Map<String, Object> map = new HashMap<>();
        map.put("error_code", 0);
        map.put("data", data);
        return ResponseEntity.ok().body(map);
    }

//    protected ResponseEntity fail(boolean result, String successMessage, String failMessage) {
//        ResponseEntity.ok().body()
//
//    }









}
